const http = require('http')
const url = require('url')
const querystring = require('querystring')
const https = require('https')

const server = http.createServer((request, response) => {
  console.log(request);
  console.log(response);
  console.log(111);
  console.log(222);
  console.log(22233);
  // response.writeHead(200, {
  //   'content-type': 'text/html'
  // })
  response.writeHeader(200, {
    'content-type': 'application/json;charset=utf-8',
    'Access-Control-Allow-Origin': '*'
  })
  let data = ''
  https.get('https://www.xiaomiyoupin.com/mtop/mf/resource/data/list', (res) => {
    res.on('data', (chunk) => {
      data += chunk
    })
    res.on('end', () => {
      response.writeHead(200, {
        'content-type': 'application/json;charset=utf-8'
      })
      console.log('data=', );
      console.log('data=22222', );
      response.end(data)
    })
  })

  // let data = ''
  // const urlStr = 'http://localhost:8080' + request.url
  // const myURL = new URL(urlStr);
  //
  // console.log('myURL', myURL);
  // const newSearchParams = new URLSearchParams(myURL.searchParams);
  // console.log('sss', newSearchParams.toString());
  // request.on('data', (chunk) => {
  //   console.log('chunk', chunk);
  //   data += chunk
  // })
  //
  // request.on('end', () => {
  //   response.writeHead(200, {
  //     'content-type': 'application/json;charset=utf-8'
  //   })
  //   console.log('data=', );
  //   response.end(JSON.stringify(querystring.parse(querystring.unescape(newSearchParams))))
  // })

})

server.listen(8080, () => {
  console.log('http://localhost:8080/');
})
